<?php

namespace app\admin\controller;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\facade\Db;
use think\App;

/**
 * @ControllerAnnotation(title="商品表")
 */
class Goods extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Goods();
        
        $this->assign('getStatusList', $this->model->getStatusList());

    }

    //获取后缀
    public function getSuffix($picname){
        $pos = strrpos($picname,'.');
        $suffix = substr($picname,$pos+1);
        return $suffix;
    }

    public function upload(){
        if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['file'])) {
            $file = $_FILES['file'];
            $getSuffix = $this->getSuffix($file['name']);
            $targetDir = "uploads/"; // 文件存储目录，确保目录存在且可写
            $targetFile = $targetDir . basename(md5($file['name'].time())).'.'.$getSuffix; // 目标文件路径和名称
            if (move_uploaded_file($file['tmp_name'], $targetFile)) {
                echo $targetFile; // 返回成功消息给客户端
            } else {
                echo "文件上传失败"; // 返回错误消息给客户端
            }
        } else {
            echo "无效的请求"; // 非POST请求或没有文件字段的情况返回错误消息给客户端
        }
    }

    
}